<?php 

class GoodsController extends AuthController{
    
    private $model;

    public function __init(){
       
     // 为了不覆盖父类authcontroller里面的init方法
    	parent::__init();
    	$this->model=K('Goods');
 }

// 显示首页
  public function index(){
  	// 调用分页类
    $total=$this->model->count();
     $pageobj=new Page($total,10,2);
     // 显示分页
    $page= $pageobj->show();
     $this->assign('page',$page);
     // 算出limit的位置
     $limit=$pageobj->limit(); 
  
   $data=$this->model->where("is_delete=0")->order('add_time desc')->limit($limit)->all();
   $this->assign('data',$data);
// p($data);die;
     $this->display();

  }

  
  // 添加商品
public function add(){


   if(IS_POST){
    // 调用模型添加操作
    if(!$this->model->addData()){
      $this->error($this->model->error);
    }
    $this->success('添加成功',U('index'));
   }
 
   // 处理所属分类
   $cateData=k('Category')->all();
   $cateData=Data::tree($cateData,'cname','cid','pid');
   $this->assign('cateData',$cateData);


   // 获得所属类型
   $type=K('Type')->all();
   $this->assign('type',$type);
   // 获得所属品牌
   $brand=K('Brand')->all();
   $this->assign('brand',$brand);

 
	$this->display();
}


// 编辑商品
public function edit(){
if(IS_POST){   

// 从隐藏域获得goods_id值                                                        
   $goods_id=Q('post.goods_id',0,'intval');
   // p($goods_id);die;
   if(!$this->model->edit($goods_id)){
       $this->error($this->model->error);
   }

 
   $this->success('修改成功',U('index'));
  }

// 获取旧数据
  $goods_id=Q('get.goods_id',0,'intval');
// 商品表旧数据
$olddata=$this->model->where("goods_id={$goods_id}")->find();
  $this->assign('olddata',$olddata);

// 详情表旧数据
  $detailData=K('Detail')->where("gid={$goods_id}")->find();

  $big=explode(',',$detailData['big']);
 // p($detailData);die;
  // p($big);die;
   $this->assign('big',$big);

$this->assign('detailData',$detailData);
// p($detail);die;
  
// 获取商品属性表表旧数据
 $goodsAttr=K('GoodsAttr')->where("gid={$goods_id}")->find();
$this->assign('goodsAttr',$goodsAttr);
  $type_id=$olddata['type_id'];
  // p($type_id);die;
  // 商品规格
$specdata = K('Attribute')->where("type_id={$type_id} and class=1")->all();
    foreach($specdata as $k => $v){
      $specdata[$k]['attr_value'] = explode(',', $v['attr_value']);
    }
    $this->assign('specdata',$specdata); 
    // var_dump($specdata);die;
    //商品属性
    $attrdata = K('Attribute')->where("type_id={$type_id} and class=0")->all();
    foreach($attrdata as $k => $v){
      $attrdata[$k]['attr_value'] = explode(',', $v['attr_value']);
    }
    $this->assign('attrdata',$attrdata); 
    //商品规格选中的情况
    $attr_value=K('GoodsAttr')->where("gid={$goods_id}")->getField('attr_value',true);
    // p($attr_value);die;
    $this->assign('attr_value',$attr_value);
    //附加价格
    $GoodsAttr=K('GoodsAttr')->where("gid={$goods_id}")->all();
    $this->assign('GoodsAttr',$GoodsAttr);




 // 处理所属分类
   $cateData=k('Category')->all();
   $cateData=Data::tree($cateData,'cname','cid','pid');
   $this->assign('cateData',$cateData);

// 获得所属类型
   $type=K('Type')->all();
   $this->assign('type',$type);
   // 获得所属品牌
   $brand=K('Brand')->all();
   $this->assign('brand',$brand);


$this->display();

}

// 异步获取规格和属性
public function getAttrSpec(){
  if(!IS_AJAX)  $this->error('非法请求');
  $type_id=Q('post.type_id',0,'intval');
  // 通过类型属性表获得对应的数据
  $data=K('Attribute')->where("type_id={$type_id}")->all();
  foreach($data as $k =>$v){
    $data[$k]['attr_value']=explode(',',$v['attr_value']);

  }
  // p($type_id);die;
  $this->ajax($data);
}


// 列表图上传
public function upload(){
  $upload=new Upload('Upload/'.date('Ymd'));
  $file=$upload->upload();
   if (empty($file)) {
      $this->ajax('上传失败');
     } else {
      $data = $file[0];
    
      $this->ajax($data);
     }
}


// 图册上传(缩略图上传)

public function uploads(){
  $upload=new Upload('Upload/'.date('Ymd'));
  $file=$upload->upload();
   if (empty($file)) {
      $this->ajax('上传失败');
     } else {
      $data = $file[0];
      // 处理缩略图
     // 组合缩略图路径
      // $data['small']=
      
      $this->ajax($data);
     }
}


/**
   * 异步删除图片
   */
  public function delImg(){
      $path = Q('post.path');
    //删除图片
    unlink($path);
    
  }


// 删除到回收站

public function del(){
 $goods_id=Q('get.goods_id',0,'intval');
 // p($goods_id);die;
 
 $_POST['is_delete']=1;
 $this->model->where("goods_id='$goods_id'")->update();
 
   $this->success('删除成功');

}
// 恢复
public function regain(){
 $goods_id=Q('get.goods_id',0,'intval');
 // p($goods_id);die;
 
 $_POST['is_delete']=0;
 $this->model->where("goods_id='$goods_id'")->update();
 
   $this->success('恢复成功');

}
  

  // 到回收站真正的删除 

  public function real_del(){
    $goods_id=Q('get.goods_id',0,'intval'); 
    
    $picData = $this->model->where("goods_id=$goods_id")->find();
    unlink($picData['goods_img']);
   $this->model->where("goods_id='$goods_id'")->delete();

   $detailData = K('Detail')->where("gid={$goods_id}")->find();

     // 要删除先删除图片再删除数据库里的路径
      // 小图删除
   // $samllData = explode(',', $detailData['small']);
   // foreach ($samllData as $v){
   //   unlink($v);
   // }
     // 中图删除
   // $mediumData = explode(',', $detailData['medium']);
   // foreach ($mediumData as $v){
   //   unlink($v);
   // }
      // 大图删除
    $bigData = explode(',', $detailData['big']);
   foreach ($bigData as $v){
     unlink($v);
   }
   K('Detail')->where("gid='$goods_id'")->delete();

   K('Shoplist')->where("gid='$goods_id'")->delete();

    K('GoodsAttr')->where("gid='$goods_id'")->delete();
  
     $this->success('删除成功');
   
 
}
 
// 回收站
public function recycle(){
   $data=$this->model->where("is_delete=1")->all();
    $this->assign('data',$data); 
	$this->display();
}

// 清空所有 
public function del_all(){
     $this->model->where("is_delete=1")->delete();
   
     $this->success('删除成功');

}




}





 ?>